package main

import (
	"encoding/base64"
	"io/ioutil"
	"log"
	"os"

	"gopkg.in/alecthomas/kingpin.v2"
)

var (
	PlainFile  = kingpin.Flag("input", "original plain text file").Short('i').Default("auth.json").String()
	EncodeFile = kingpin.Flag("output", "base64 encoded file").Short('o').Default("auth.enc").String()
)

func main() {
	kingpin.Parse()
	plain, err := ioutil.ReadFile(*PlainFile)
	if err != nil {
		log.Panic(err)
	}
	enc := base64.StdEncoding.EncodeToString(plain)
	if _, err := os.Stat(*EncodeFile); err == nil {
		os.Remove(*EncodeFile)
	}
	file, err := os.OpenFile(*EncodeFile, os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		log.Panic(err)
	}
	defer file.Close()
	file.WriteString(enc)
}
